﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.PRM.AppCode.Lib;

namespace mojoPortal.Web.RS.PRM.Controls
{
    public partial class PropertyControl : RSBaseUserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["action"] == "add")
            {
                btnEdit.Visible = false;
                btnSave.Visible = true;
            }
            else
            {
                SetControlEnabledState(false);
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string prevurl = string.Empty;
            if (Session["PreviousURL"] != null)
                prevurl = Session["PreviousURL"].ToString();
            else
                prevurl = Request.UrlReferrer.ToString();
            Response.Redirect(prevurl);
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                SetControlEnabledState(true);
                btnEdit.Visible = false;
                btnSave.Visible = true;
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {

            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);

        }
        public void SetControlEnabledState(bool bstate)
        {
            RSPRMCommonFunctions.SetControlsEnabledState(this, bstate);
        }
        public bool LoadPropertyData(int companyid, int propertyid)
        {
            bool bHasValue = false;
            txtPropertyID.Text = propertyid.ToString();
            RSPRMDataLookupLoader.LoadTypeDDL(companyid, "PROPERTY_CAT", ddlPropertyType);
            RSPRMDataLookupLoader.LoadPropertyOwnerDDL(companyid, ddlPropertyOwner);

            if (companyid > 0 && propertyid > 0)
            {
                String sql = "exec rs_uspGetPropertyByPropertyID @CompanyID, @PropertyID";
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(sql, myConnection);
                SqlDataReader myDataReader;

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@CompanyID";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@PropertyID";
                param.Value = propertyid;
                myCommand.Parameters.Add(param);

                myConnection.Open();
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                if (myDataReader.HasRows)
                {
                    myDataReader.Read();

                    txtStreet.Text = myDataReader["Street"].ToString();
                    txtCity.Text = myDataReader["City"].ToString();
                    txtCounty.Text = myDataReader["County"].ToString();
                    txtState.Text = myDataReader["State"].ToString();
                    txtCountry.Text = myDataReader["Country"].ToString();
                    txtZip.Text = myDataReader["Zip"].ToString();
                    txtSubdivisionName.Text = myDataReader["SubdivisionName"].ToString();
                    txtSqFt.Text = myDataReader["SqFt"].ToString();
                    txtBedNum.Text = myDataReader["BedNum"].ToString();
                    txtBathNum.Text = myDataReader["BathNum"].ToString();
                    txtYearBuilt.Text = myDataReader["YearBuilt"].ToString();

                    txtAppraisedValue.Text = myDataReader["AppraisedValue"].ToString();
                    txtAssessedValue.Text = myDataReader["AssessedValue"].ToString();
                    txtDescription.Text = myDataReader["Description"].ToString();

                    txtElementarySchool.Text = myDataReader["ElementarySchool"].ToString();
                    txtMiddleSchool.Text = myDataReader["MiddleSchool"].ToString();
                    txtHighSchool.Text = myDataReader["HighSchool"].ToString();

                    //if (myDataReader["CurrentTenantID"] != DBNull.Value)
                    //{
                    txtTenantID.Text = myDataReader["CurrentTenantID"].ToString();
                    //}                
                    //if (myDataReader["CurrentLeaseID"] != DBNull.Value)
                    //{
                    txtLeaseID.Text = myDataReader["CurrentLeaseID"].ToString();
                    //}
                    if (myDataReader["IsOccupied"] != DBNull.Value)
                    {
                        bool isOccupied = Convert.ToBoolean(myDataReader["IsOccupied"]);
                        chkIsOccupied.Checked = isOccupied;
                    }
                    if (myDataReader["OwnerID"] != DBNull.Value)
                    {
                        string ownerId = myDataReader["OwnerID"].ToString();
                        ddlPropertyOwner.SelectedValue = ownerId;
                    }
                    if (myDataReader["PropertyTypeID"] != DBNull.Value)
                    {
                        string propTypeId = myDataReader["PropertyTypeID"].ToString();
                        ddlPropertyType.SelectedValue = propTypeId;
                    }
                    if (myDataReader["PhotoPath"] != DBNull.Value)
                    {
                        imgPropertyPhoto.ImageUrl = myDataReader["PhotoPath"].ToString();
                    }

                    btnEdit.Visible = true;
                    bHasValue = true;

                }
                myConnection.Close();
            }
            return bHasValue;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int PropertyID = int.Parse(txtPropertyID.Text);
            float BedNum = txtBedNum.Text == "" ? 0 : float.Parse(txtBedNum.Text);
            float BathNum = txtBathNum.Text == "" ? 0 : float.Parse(txtBathNum.Text);
            int SqFt = txtSqFt.Text == "" ? 0 : int.Parse(txtSqFt.Text);
            int YearBuilt = txtYearBuilt.Text == "" ? 0 : int.Parse(txtYearBuilt.Text);
            int AppraisedValue = txtAppraisedValue.Text == "" ? 0 : int.Parse(txtAppraisedValue.Text);
            int AssessedValue = txtAssessedValue.Text == "" ? 0 : int.Parse(txtAssessedValue.Text);
            int PropertyTypeID = int.Parse(ddlPropertyType.SelectedValue);
            int OwnerID = int.Parse(ddlPropertyOwner.SelectedValue);
            string Street = txtStreet.Text;
            string City = txtCity.Text;
            string State = txtState.Text;
            string Zip = txtZip.Text;
            string County = txtCounty.Text;
            string Country = txtCountry.Text;
            string SubdivisionName = txtSubdivisionName.Text;
            string Description = txtDescription.Text;
            bool IsOccupied = chkIsOccupied.Checked;
            string ElementarySchool = txtElementarySchool.Text;
            string MiddleSchool = txtMiddleSchool.Text;
            string HighSchool = txtHighSchool.Text;

            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                SqlCommand sqlCmd = new SqlCommand();

                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@BedNum", SqlDbType.Float, 0, ParameterDirection.Input, BedNum);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@BathNum", SqlDbType.Float, 0, ParameterDirection.Input, BathNum);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@SqFt", SqlDbType.Int, 0, ParameterDirection.Input, SqFt);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@YearBuilt", SqlDbType.Int, 0, ParameterDirection.Input, YearBuilt);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@AppraisedValue", SqlDbType.Int, 0, ParameterDirection.Input, AppraisedValue);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@AssessedValue", SqlDbType.Int, 0, ParameterDirection.Input, AssessedValue);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Street", SqlDbType.NVarChar, 250, ParameterDirection.Input, Street);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@City", SqlDbType.NVarChar, 50, ParameterDirection.Input, City);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@State", SqlDbType.NVarChar, 50, ParameterDirection.Input, State);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@County", SqlDbType.NVarChar, 50, ParameterDirection.Input, County);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Country", SqlDbType.NVarChar, 50, ParameterDirection.Input, Country);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Zip", SqlDbType.NVarChar, 10, ParameterDirection.Input, Zip);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@SubdivisionName", SqlDbType.NVarChar, 250, ParameterDirection.Input, SubdivisionName);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Description", SqlDbType.NVarChar, 250, ParameterDirection.Input, Description);

                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ElementarySchool", SqlDbType.NVarChar, 250, ParameterDirection.Input, ElementarySchool);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@MiddleSchool", SqlDbType.NVarChar, 250, ParameterDirection.Input, MiddleSchool);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@HighSchool", SqlDbType.NVarChar, 250, ParameterDirection.Input, HighSchool);

                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ManagerID", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSEmployeeId);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@OwnerID", SqlDbType.Int, 0, ParameterDirection.Input, OwnerID);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyTypeID", SqlDbType.Int, 0, ParameterDirection.Input, PropertyTypeID);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyID", SqlDbType.Int, 0, ParameterDirection.Input, PropertyID);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@IsOccupied", SqlDbType.Bit, 0, ParameterDirection.Input, IsOccupied);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 25, ParameterDirection.Input, userIdentity.RSCurrentUser);
                SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspSaveProperty");
                SQLHelper.ExecuteScalarCmd(sqlCmd);

                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                if (returnValue > 0)
                {
                    if (FileUpload1.HasFile)
                    {
                        string folder = userIdentity.RSPhotoPath;
                        string fileLocalPath = Server.MapPath(folder);
                        string photoname = FileUpload1.FileName.Replace(" ", "_");
                        string photopath = folder + returnValue + "_" + photoname;
                        string filename_localpath = fileLocalPath + returnValue + "_" + photoname;
                        FileUpload1.SaveAs(filename_localpath);

                        PropertyID = returnValue;
                        string SQL = "UPDATE rs_tblProperty SET PhotoPath=@PhotoPath WHERE PropertyID=@PropertyID AND CompanyID=@CompanyId";
                        sqlCmd = new SqlCommand();
                        SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                        SQLHelper.AddParamToSQLCmd(sqlCmd, "@PhotoPath", SqlDbType.NVarChar, 150, ParameterDirection.Input, photopath);
                        SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyID", SqlDbType.Int, 0, ParameterDirection.Input, PropertyID);
                        SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
                        SQLHelper.SetCommandType(sqlCmd, CommandType.Text, SQL);
                        SQLHelper.ExecuteScalarCmd(sqlCmd);
                    }

                    string prevurl = string.Empty;
                    if (Session["PreviousURL"] != null)
                        prevurl = Session["PreviousURL"].ToString();
                    else
                        prevurl = Request.UrlReferrer.ToString();
                    ucModalActionMessageControl.ShowModal("Property Successfully Saved!", prevurl, true);
                    btnSave.Enabled = false;
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);

        }
    }
}